<div class="layui-card-body" >

    <table class="layui-table text-center" id="categorylist" lay-filter="categorylist"
           list-url="{:url('getcategory')}"
           edit-url="{:url('editcategory')}"
           del-url="{:url('delcategory')}"
           addcategory-url="{:url('addcategory')}"></table>
    <form class="layui-form" action="{:url('category')}"
         method="post">
        <button type="button" class="layui-btn layui-btn-normal" lay-filter="addcategory"  lay-submit>添加根分类</button>
    </form>
</div>
<script type="text/html" id="categorybarDemo">
    <a class="layui-btn  layui-btn-xs" lay-event="addcategory"><i class="fa fa-laptop"></i>添加</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"> <i class="fa fa-trash-o"></i> 删除</a>
</script>

<script type="text/javascript">
    layui.use(['form', 'layer', 'table'], function () {
        var form = layui.form
            , table = layui.table
            , $ = layui.$
            ,layer=layui.layer
            , data = [];

        form.on('submit(addcategory)', function(data){
            layer.prompt({title: '根分类名称，并确认', formType: 0}, function(text, index){
                $.ajax({
                    type: "post",
                    url: "{:url('addcategory')}",
                    async: true,
                    data:{
                        title:text,
                        pid:0
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, {
                                time: 1500,
                                icon: 5
                            });

                        } else {
                            table.reload('categorylist', {
                                url: "{:url('getcategory')}"
                                , page: false
                                ,height:400
                                , where: {} //设定异步数据接口的额外参数
                                //,height: 300
                            });
                            layer.msg(res.msg, {
                                time: 1500,
                                icon: 1
                            });

                        }
                        layer.close(index);
                    }
                });

            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        table.render({
            elem: '#categorylist'
            , url: "{:url('getcategory')}"
            , loading: true
            , page: false
            ,height:400
            , cols: [[
                 {field: 'sort', width: 100,title: '分类排序', edit: 'text'}
                , {field: 'title',width: 200, title: '分类名称', templet: '<div>{{d.html}}{{d.title}}</div>', edit: 'text'}
                , {fixed: 'right', width: 130, title: '操作', toolbar: '#categorybarDemo'}
            ]]
        });


    });
</script>






